/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package feed.dao;

import feed.conexion.DataBaseInstance;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author lancaster
 */
public class TotalesDao {
    protected Connection getConnection() {
        return DataBaseInstance.getInstanceConnection();
        
    }
        int idmovimiento=0;
        int sumadebe=0;
        int sumahaber=0;

    public int getIdmovimiento() {
        return idmovimiento;
    }

    public void setIdmovimiento(int idmovimiento) {
        this.idmovimiento = idmovimiento;
    }

    public int getSumadebe() {
        return sumadebe;
    }

    public void setSumadebe(int sumadebe) {
        this.sumadebe = sumadebe;
    }

    public int getSumahaber() {
        return sumahaber;
    }

    public void setSumahaber(int sumahaber) {
        this.sumahaber = sumahaber;
    }
        
public void IngresarTotal(){
       this.obtenerIdmov();
       try {            
            PreparedStatement pstm = getConnection().prepareStatement("insert into APP.totales(idmovimiento, debe, haber) values(?,?,?)");            
            pstm.setInt(1,idmovimiento);
            pstm.setInt(2,sumadebe);
            pstm.setInt(3,sumahaber);
            pstm.execute();
            pstm.close();            
         }catch(SQLException e){
         System.out.println(e);
      }
   }
public void obtenerIdmov(){
    ResultSet result = null;
       try {            
            String query ="select max(id)as ultima from APP.MOVIMIENTO";            
            PreparedStatement stmt = getConnection().prepareStatement(query);
            
            result = stmt.executeQuery();
            if (!result.next()) {
                throw new SQLException();
            }
            
            this.setIdmovimiento(result.getInt("ultima"));
            
            
            result.close();
            stmt.close();
           System.out.println("ID movimiento recivido: "+this.getIdmovimiento()); 
         }catch(SQLException e){
         System.out.println(e);
      }

   }
public void sumarTotal2(){
    ResultSet result = null;
       try {            
            String query ="select sum(debe) as sumadebe,sum(haber) as sumahaber from APP.DETALLEMOVIMIENTO c where idmovimiento=(select max(id) from app.movimiento)";            
            PreparedStatement stmt = getConnection().prepareStatement(query);
            
            result = stmt.executeQuery();
            if (!result.next()) {
                throw new SQLException();
            }
            
            this.setSumadebe(result.getInt("sumadebe"));
            this.setSumahaber(result.getInt("sumahaber"));
            
            result.close();
            stmt.close();
            
         }catch(SQLException e){
         System.out.println(e);
      }
     this.obtenerIdmov();
    }
}